package com.sweetline.pub.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * cookie util
 * 
 * 2016-8-29
 * @author xiacj
 *
 */
public class CookieUtil {

	public static void addCookie(HttpServletResponse response,String name,String val){
		
		Cookie cookie = new Cookie(name,val);
		cookie.setMaxAge(365*24*60*60);
		cookie.setPath("/");
		response.addCookie(cookie);
	}
	
	public static String getCookie(HttpServletRequest request,String name){
		
		Cookie[] cookies = request.getCookies();
		if(cookies != null){
			for(Cookie cookie : cookies){
				if(name.equals(cookie.getName())){
					return cookie.getValue();
				}
			}
		}
		
		return null;
	}
}
